<?php
/**
 * Created by PhpStorm.
 * User: shutao
 * Date: 2018/9/22
 * Time: 09:28
 */

namespace wechat\models;

use tt\common\components\TtActiveRecord;
use ttcommon\base\Arrays;
use yii\db\Migration;

/**
 * 为公众号指定关联词回复：比如将某篇文章指定给公众号的 subscribe 关键词；
 * 那么当用户订阅此公众号时，就会把这些文章发送给微信服务器；
 *
 * @property integer $id
 * @property integer $wc_id
 * @property string $keyword
 * @property integer $start
 * @property integer $end
 * @property integer $status
 * @property integer $c_type
 * @property integer $c_id
 */
class WeKeyword extends TtActiveRecord
{

    public static function tableName()
    {
        return 'we_keyword';
    }

    public function createTableWithName(Migration $migration, $tableName)
    {
        $migration->createTable($tableName, [
            'id' => $migration->primaryKey()->unsigned(),
            'wc_id' => $migration->integer()->notNull()->defaultValue(0)->unsigned()->comment($this->getAttributeLabel('wc_id')),
            'keyword' => $migration->char(20)->notNull()->defaultValue('')->comment($this->getAttributeLabel('keyword')),
            'start' => $migration->integer()->notNull()->defaultValue(0)->comment($this->getAttributeLabel('start')),
            'end' => $migration->integer()->notNull()->defaultValue(0)->comment($this->getAttributeLabel('end')),
            'status' => $migration->tinyInteger(1)->notNull()->defaultValue(self::STATUS_ACTIVE)->comment($this->getAttributeLabel('status')),
            'c_type' => $migration->tinyInteger(1)->notNull()->defaultValue(0)->comment($this->getAttributeLabel('type')),
            'c_id' => $migration->integer()->notNull()->defaultValue(0)->comment($this->getAttributeLabel('c_id'))
        ]);

        $migration->createIndex('use', $tableName, ['wc_id', 'keyword', 'status']);
    }

    public function attributeLabels()
    {
        return [
            'wc_id' => '公众号 ID',
            'keyword' => '关键词',
            'start' => '启用时间',
            'end' => '停用时间',
            'status' => '状态',
            'c_type' => '回复内容类型',
            'c_id' => '回复内容',
        ];
    }

    const C_TYPE_CONTENT = 0;
    const C_TYPE_BLOG_ARTICLE = 1;

    public static function cTypeText($type = null)
    {
        static $text = [
            self::C_TYPE_CONTENT => '内容回复',
            self::C_TYPE_BLOG_ARTICLE => '文章(TODO)'
        ];
        return Arrays::constText($text, $type);
    }

    const STATUS_DISABLE = 0;
    const STATUS_ACTIVE = 1;

    public static function statusText($status = null)
    {
        static $text = [
            self::STATUS_DISABLE => '停用',
            self::STATUS_ACTIVE => '使用'
        ];
        return Arrays::constText($text, $status);
    }

    public function rules()
    {
        return [
            [
                ['wc_id', 'start', 'end', 'status', 'c_id'], 'integer'
            ],
            ['keyword', 'string', 'length' => [1, 20]],
            ['start', 'default', 'value' => 0],
            ['end', 'default', 'value' => 0],
            ['wc_id', 'compare', 'compareValue' => 1, 'operator' => '>='],
            ['c_id', 'compare', 'compareValue' => 1, 'operator' => '>='],
        ];
    }

    public function scenarios()
    {
        return [
            self::SCENARIO_DEFAULT => [
                'wc_id', 'keyword', 'start', 'end', 'status', 'c_type', 'c_id'
            ]
        ];
    }

    public function beforeSave($insert)
    {
        return parent::beforeSave($insert);
    }
}